{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# tsp_circuit_board"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/constraint_solver/tsp_circuit_board.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/ortools/constraint_solver/samples/tsp_circuit_board.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "Simple Travelling Salesperson Problem (TSP) on a circuit board.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from ortools.constraint_solver import routing_enums_pb2\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "\n",
    "def create_data_model():\n",
    "    \"\"\"Stores the data for the problem.\"\"\"\n",
    "    data = {}\n",
    "    # Locations in block units\n",
    "    data[\"locations\"] = [\n",
    "        # fmt: off\n",
    "      (288, 149), (288, 129), (270, 133), (256, 141), (256, 157), (246, 157),\n",
    "      (236, 169), (228, 169), (228, 161), (220, 169), (212, 169), (204, 169),\n",
    "      (196, 169), (188, 169), (196, 161), (188, 145), (172, 145), (164, 145),\n",
    "      (156, 145), (148, 145), (140, 145), (148, 169), (164, 169), (172, 169),\n",
    "      (156, 169), (140, 169), (132, 169), (124, 169), (116, 161), (104, 153),\n",
    "      (104, 161), (104, 169), (90, 165), (80, 157), (64, 157), (64, 165),\n",
    "      (56, 169), (56, 161), (56, 153), (56, 145), (56, 137), (56, 129),\n",
    "      (56, 121), (40, 121), (40, 129), (40, 137), (40, 145), (40, 153),\n",
    "      (40, 161), (40, 169), (32, 169), (32, 161), (32, 153), (32, 145),\n",
    "      (32, 137), (32, 129), (32, 121), (32, 113), (40, 113), (56, 113),\n",
    "      (56, 105), (48, 99), (40, 99), (32, 97), (32, 89), (24, 89),\n",
    "      (16, 97), (16, 109), (8, 109), (8, 97), (8, 89), (8, 81),\n",
    "      (8, 73), (8, 65), (8, 57), (16, 57), (8, 49), (8, 41),\n",
    "      (24, 45), (32, 41), (32, 49), (32, 57), (32, 65), (32, 73),\n",
    "      (32, 81), (40, 83), (40, 73), (40, 63), (40, 51), (44, 43),\n",
    "      (44, 35), (44, 27), (32, 25), (24, 25), (16, 25), (16, 17),\n",
    "      (24, 17), (32, 17), (44, 11), (56, 9), (56, 17), (56, 25),\n",
    "      (56, 33), (56, 41), (64, 41), (72, 41), (72, 49), (56, 49),\n",
    "      (48, 51), (56, 57), (56, 65), (48, 63), (48, 73), (56, 73),\n",
    "      (56, 81), (48, 83), (56, 89), (56, 97), (104, 97), (104, 105),\n",
    "      (104, 113), (104, 121), (104, 129), (104, 137), (104, 145), (116, 145),\n",
    "      (124, 145), (132, 145), (132, 137), (140, 137), (148, 137), (156, 137),\n",
    "      (164, 137), (172, 125), (172, 117), (172, 109), (172, 101), (172, 93),\n",
    "      (172, 85), (180, 85), (180, 77), (180, 69), (180, 61), (180, 53),\n",
    "      (172, 53), (172, 61), (172, 69), (172, 77), (164, 81), (148, 85),\n",
    "      (124, 85), (124, 93), (124, 109), (124, 125), (124, 117), (124, 101),\n",
    "      (104, 89), (104, 81), (104, 73), (104, 65), (104, 49), (104, 41),\n",
    "      (104, 33), (104, 25), (104, 17), (92, 9), (80, 9), (72, 9),\n",
    "      (64, 21), (72, 25), (80, 25), (80, 25), (80, 41), (88, 49),\n",
    "      (104, 57), (124, 69), (124, 77), (132, 81), (140, 65), (132, 61),\n",
    "      (124, 61), (124, 53), (124, 45), (124, 37), (124, 29), (132, 21),\n",
    "      (124, 21), (120, 9), (128, 9), (136, 9), (148, 9), (162, 9),\n",
    "      (156, 25), (172, 21), (180, 21), (180, 29), (172, 29), (172, 37),\n",
    "      (172, 45), (180, 45), (180, 37), (188, 41), (196, 49), (204, 57),\n",
    "      (212, 65), (220, 73), (228, 69), (228, 77), (236, 77), (236, 69),\n",
    "      (236, 61), (228, 61), (228, 53), (236, 53), (236, 45), (228, 45),\n",
    "      (228, 37), (236, 37), (236, 29), (228, 29), (228, 21), (236, 21),\n",
    "      (252, 21), (260, 29), (260, 37), (260, 45), (260, 53), (260, 61),\n",
    "      (260, 69), (260, 77), (276, 77), (276, 69), (276, 61), (276, 53),\n",
    "      (284, 53), (284, 61), (284, 69), (284, 77), (284, 85), (284, 93),\n",
    "      (284, 101), (288, 109), (280, 109), (276, 101), (276, 93), (276, 85),\n",
    "      (268, 97), (260, 109), (252, 101), (260, 93), (260, 85), (236, 85),\n",
    "      (228, 85), (228, 93), (236, 93), (236, 101), (228, 101), (228, 109),\n",
    "      (228, 117), (228, 125), (220, 125), (212, 117), (204, 109), (196, 101),\n",
    "      (188, 93), (180, 93), (180, 101), (180, 109), (180, 117), (180, 125),\n",
    "      (196, 145), (204, 145), (212, 145), (220, 145), (228, 145), (236, 145),\n",
    "      (246, 141), (252, 125), (260, 129), (280, 133)\n",
    "        # fmt: on\n",
    "    ]\n",
    "    data[\"num_vehicles\"] = 1\n",
    "    data[\"depot\"] = 0\n",
    "    return data\n",
    "\n",
    "\n",
    "def compute_euclidean_distance_matrix(locations):\n",
    "    \"\"\"Creates callback to return distance between points.\"\"\"\n",
    "    distances = {}\n",
    "    for from_counter, from_node in enumerate(locations):\n",
    "        distances[from_counter] = {}\n",
    "        for to_counter, to_node in enumerate(locations):\n",
    "            if from_counter == to_counter:\n",
    "                distances[from_counter][to_counter] = 0\n",
    "            else:\n",
    "                # Euclidean distance\n",
    "                distances[from_counter][to_counter] = int(\n",
    "                    math.hypot((from_node[0] - to_node[0]), (from_node[1] - to_node[1]))\n",
    "                )\n",
    "    return distances\n",
    "\n",
    "\n",
    "def print_solution(manager, routing, solution):\n",
    "    \"\"\"Prints solution on console.\"\"\"\n",
    "    print(f\"Objective: {solution.ObjectiveValue()}\")\n",
    "    index = routing.Start(0)\n",
    "    plan_output = \"Route:\\n\"\n",
    "    route_distance = 0\n",
    "    while not routing.IsEnd(index):\n",
    "        plan_output += f\" {manager.IndexToNode(index)} ->\"\n",
    "        previous_index = index\n",
    "        index = solution.Value(routing.NextVar(index))\n",
    "        route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)\n",
    "    plan_output += f\" {manager.IndexToNode(index)}\\n\"\n",
    "    print(plan_output)\n",
    "    plan_output += f\"Objective: {route_distance}m\\n\"\n",
    "\n",
    "\n",
    "def main():\n",
    "    \"\"\"Entry point of the program.\"\"\"\n",
    "    # Instantiate the data problem.\n",
    "    data = create_data_model()\n",
    "\n",
    "    # Create the routing index manager.\n",
    "    manager = pywrapcp.RoutingIndexManager(\n",
    "        len(data[\"locations\"]), data[\"num_vehicles\"], data[\"depot\"]\n",
    "    )\n",
    "\n",
    "    # Create Routing Model.\n",
    "    routing = pywrapcp.RoutingModel(manager)\n",
    "\n",
    "    distance_matrix = compute_euclidean_distance_matrix(data[\"locations\"])\n",
    "\n",
    "    def distance_callback(from_index, to_index):\n",
    "        \"\"\"Returns the distance between the two nodes.\"\"\"\n",
    "        # Convert from routing variable Index to distance matrix NodeIndex.\n",
    "        from_node = manager.IndexToNode(from_index)\n",
    "        to_node = manager.IndexToNode(to_index)\n",
    "        return distance_matrix[from_node][to_node]\n",
    "\n",
    "    transit_callback_index = routing.RegisterTransitCallback(distance_callback)\n",
    "\n",
    "    # Define cost of each arc.\n",
    "    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)\n",
    "\n",
    "    # Setting first solution heuristic.\n",
    "    search_parameters = pywrapcp.DefaultRoutingSearchParameters()\n",
    "    search_parameters.first_solution_strategy = (\n",
    "        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC\n",
    "    )\n",
    "\n",
    "    # Solve the problem.\n",
    "    solution = routing.SolveWithParameters(search_parameters)\n",
    "\n",
    "    # Print solution on console.\n",
    "    if solution:\n",
    "        print_solution(manager, routing, solution)\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
